import { HashRouter as Router, Switch, Route, Redirect } from 'react-router-dom';
import React, { Suspense } from 'react';
import path from 'path';
import routes from '@/routerConfig';
import PageLoading from '@/components/PageLoading';

const RouteItem = (props) => {
    const {
        redirect, path: routePath, component, key,
    } = props;
    if (redirect) {
        return (
            <Redirect
                exact
                key={key}
                from={routePath}
                to={redirect}
            />
        );
    }
    // }
    // return (
    //   <Route
    //     key={key}
    //     component={component}
    //     path={routePath}
    //   />
    // );
};


const PrivateRoute = ({ component: Component, ...props }) =>
// 解构赋值 将 props 里面的 component 赋值给 Component
    (<Route
        {...props}
        render={(p) => {
            // <Component />

            const login = window.isRegister;
            if (login || p.location.pathname == '/dashboard'|| p.location.pathname == '/index') {
                return <Component />;
            } // 没有登录就重定向至登录页面
            if (p.location.pathname != '/') {
                alert('该系统未注册，请先联系相关人员完成注册。');
            }
            return (<Redirect to={{
                pathname: '/dashboard',
            }}
            />);
        }

        }
    />);
const router = () => (
    <Router>
        <Switch>
            {routes.map((route, id) => {
                const { component: RouteComponent, children, ...others } = route;
                return (
                    <Route
                        key={id}
                        {...others}
                        component={props => (
                            children ? (
                                <RouteComponent key={id} {...props}>
                                    <Suspense fallback={<PageLoading />}>
                                        <Switch>
                                            {children.map((routeChild, idx) => {
                                                const { redirect, path: childPath, component } = routeChild;
                                                return PrivateRoute({
                                                    key: `${id}-${idx}`,
                                                    redirect,
                                                    path: childPath && path.join(route.path, childPath),
                                                    component,
                                                });
                                            })}
                                        </Switch>
                                    </Suspense>
                                </RouteComponent>
                            ) : (
                                <Suspense fallback={<PageLoading />}>
                                    {
                                        RouteItem({
                                            key: id,
                                            ...props,
                                        })
                                    }
                                </Suspense>
                            )
                        )}
                    />
                );
            })}
        </Switch>
    </Router>
);

export default router;
